package com.flexcloudsdk.services.users
{
	import com.flexcloudsdk.framework.core.CloudBase;
	import mx.rpc.Fault;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.controls.Alert;
	import org.osmf.logging.Log;
	[RemoteClass(alias="com.webcloudsdk.users.CloudUser")]
	public class CloudUser extends CloudBase
	{
		public function CloudUser() 
		{
			super();
			super.serviceName = "CloudUsers";
			super.objectId = "CloudUserHttp";
			super.addEventListener(ResultEvent.RESULT, httpResultHandler);
			super.addEventListener(FaultEvent.FAULT, httpFaultHandler);
		}
		
		
		public function cloudCall(methodName:String, parms:Array = null):void
		{
			if (useHttp)
			{
				httpService.url = appUrl + "?" + methodName;
				httpService.send();
			}
			else if (useRemoting)
			{
				remoteObject.getOperation(methodName).send();
			}
		}
			
		public function createLoginURLCall(destinationURL:String,
										   authDomain:String=null,
										   federatedIdentity:String=null,
										   attributesRequest:String=null):void
		{
			trace("createLoginURL called");
			if (useHttp)
			{	
				var callOperation:XML = <operation>createLoginURL</operation>;
				callOperation.appendChild(new XML(destinationURL));
				if (authDomain)
					callOperation.appendChild(new XML("<parameters>" + authDomain + "</parameters>"));
				if (	federatedIdentity)
					callOperation.appendChild(new XML("<parameters>" + federatedIdentity + "</parameters>"));
				if (attributesRequest)
					callOperation.appendChild(new XML("<parameters>" +  attributesRequest + "</parameters>"));
				cloudCall(callOperation);
			}
		}
		
		public function createLogoutURLCall(destinationURL:String, authDomain:String=null):void
		{
			trace("createLogoutURL");
			if (useHttp)
			{
				var callOperation:XML = <operation>createLogoutURLCall</operation>;
				callOperation.appendChild(new XML("<parameters>" + destinationURL + "</parameters>"));
				if (authDomain)
					callOperation.appendChild(new XML("<parameters>" + authDomain + "</parameters>"));
				cloudCall(callOperation);
			}				
		}
		
		public function createNewUserCall(email:String, authDomain:String, userId:String=null):void
		{
			trace("createNewUser");
			if (useHttp)
			{
				var callOperation:XML = <operation>createNewUserCall</operation>;
				callOperation.appendChild(new XML("<parameters>" + email + "</parameters>"));
				callOperation.appendChild(new XML("<parameters>" + authDomain + "</parameters>"));
				if (userId)
					callOperation.appendChild(new XML("<parameters>" + userId + "</parameters>"));
				cloudCall(callOperation);
			}				
		}
		
		public function getAuthDomain():void
		{
			trace("getAuthDomain called");
			if (useHttp)
			{	
				var callOperation:XML = <operation>getAuthDomain</operation>;
				cloudCall(callOperation);
			}
		}
		
		
		public function getDomain():void
		{
			trace("getDomain called");
			if (useHttp)
			{	
				var callOperation:XML = <operation>getDomain</operation>;
				cloudCall(callOperation);
			}
		}
		
		public function getEmail():void
		{
			trace("getEmail called");
			if (useHttp)
			{	
				var callOperation:XML = <operation>getEmail</operation>;
				cloudCall(callOperation);
			}	
		}
		
		public function getNickname():void
		{
			trace("getNickname called");
			if (useHttp)
			{	
				var callOperation:XML = <operation>getNickname</operation>;
				cloudCall(callOperation);
			}	
		}
		
		public function isUserAdmin():void
		{
			trace("isUserAdmin called");
			if (useHttp)
			{	
				var callOperation:XML = <operation>isUserAdmin</operation>;
				cloudCall(callOperation);
			}	
		}
		
		public function getCurrentUser():void
		{
			trace("getCurrentUser called");
			if (useHttp)
			{	
				var request:Object = new Object();
				request.operation = "getCurrentUser";
				httpService.request = request;
				cloudCall();
			}	
		}
		
		override protected function httpResultHandler(event:ResultEvent):void
		{
			var result:XML = event.result as XML;
			Alert.show(result.toXMLString(), "Cloud Result: CloudUser::getCurrentUser()");
		}
		
		override protected function httpFaultHandler(event:FaultEvent):void
		{
			var error:Fault = event.fault as Fault;
			trace(error.faultString);
			Alert.show(error.toString(), "Cloud Fault: CloudUser::getCurrentUser()");
		}

		
	}
}